package com.example.myapplication;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ButtonActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_page);
        Button btn = findViewById(R.id.btn_onTap);
        ImageButton state_image_button = findViewById(R.id.state_image_button);
        btn.setOnClickListener(new MyOnClickListener());
        btn.setOnLongClickListener(new MyOnLongClickListener());
        state_image_button.setBackground(setBg());
    }


    public StateListDrawable setBg(){
        StateListDrawable stateListDrawable = new StateListDrawable();
        Drawable drawable1 = this.getResources().getDrawable(R.drawable.aaaf1);
        Drawable drawable2 = this.getResources().getDrawable(R.drawable.aaaf2);
        Drawable drawable3 = this.getResources().getDrawable(R.drawable.aaaf3);
        Drawable drawable4 = this.getResources().getDrawable(R.drawable.aaaf4);
       stateListDrawable.addState(new int[] {android.R.attr.state_pressed,android.R.attr.state_enabled,},drawable1);
       stateListDrawable.addState(new int[] {android.R.attr.state_pressed,android.R.attr.state_focused,},drawable2);
       stateListDrawable.addState(new int[] {android.R.attr.state_enabled,},drawable3);
       stateListDrawable.addState(new int[] {android.R.attr.state_focused,},drawable4);
        return  stateListDrawable;
    }

    public void onClickButton(View view) {
        Log.v("VERBOSE", "onClick: 背景色的按钮");
    }

    class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btn_onTap) {
                Toast.makeText(ButtonActivity.this, "您点击了按钮：" + ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
                Log.v("VERBOSE", "onClick: 您点击了按钮");
                Log.d("DEBUG", "onClick: 您点击了按钮");
                Log.i("INFO", "onClick: 您点击了按钮");
                Log.e("ERROR", "onClick: 您点击了按钮");
                Log.w("WARN", "onClick: 您点击了按钮");
            }
        }
    }

    class MyOnLongClickListener implements View.OnLongClickListener {
        @Override
        public boolean onLongClick(View v) {
            if (v.getId() == R.id.btn_onTap) {
                Toast.makeText(ButtonActivity.this, "您长按了按钮：" + ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    }
}
